home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util1 / shell-10.lha / shell-1.0 / src / else.c next >
C/C++ Source or Header  |  1995-12-07  |  2KB  |  97 lines

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <dos/rdargs.h>
  6. #include <dos/stdio.h>
  7. #include <proto/dos.h>
  8. #include <proto/utility.h>
  9.  
  10. #define NODEBUG 1
  11.  
  12. #ifdef DEBUG
  13. #   define Dputs puts
  14. #else
  15. #   define Dputs(x)
  16. #endif
  17.  
  18. const char cont_template[] = "REST/M,IF/S,ENDIF/S,FI/S";
  19.  
  20. /* This structure is used to filter the body of the else */
  21. struct ContStruct
  22. {
  23.     char ** rest;
  24.     ULONG is_if;
  25.     ULONG is_endif;
  26.     ULONG is_fi;
  27. };
  28.  
  29. int main (int argc, char ** argv)
  30. {
  31.     struct ContStruct cont_struct;
  32.     struct RDArgs * cont_rda;
  33.     int level;
  34.     BPTR old_in, current_input;
  35.     int cond = 0;
  36.     LONG current_pos;
  37.     int c;
  38.  
  39.     level = 0;
  40.  
  41.     current_input = Cli()->cli_CurrentInput;
  42.     old_in = Input ();
  43.     SelectInput (current_input);
  44.  
  45.     c = FGetC (current_input);
  46.     Flush (current_input);
  47.     UnGetC (current_input, c);
  48.  
  49.     memset (&cont_struct, 0, sizeof(cont_struct));
  50.  
  51.     for (;;)
  52.     {
  53.     if (!(cont_rda = ReadArgs ((char *)cont_template,
  54.         (LONG *)&cont_struct, NULL)) )
  55.     {
  56.         PrintFault (IoErr(), "ContReadArgs");
  57.         cond = 10;
  58.         break;
  59.     }
  60.  
  61.     if (cont_struct.is_if)
  62.     {
  63.         Dputs ("Found if");
  64.         level ++;
  65.     }
  66.     else if (cont_struct.is_endif || cont_struct.is_fi)
  67.     {
  68.         Dputs ("Found endif/fi");
  69.         if (!level)
  70.         break;
  71.  
  72.         level --;
  73.     }
  74.     else
  75.     {
  76.         Dputs ("Line ignored");
  77.     }
  78.  
  79.     memset (&cont_struct, 0, sizeof(cont_struct));
  80.     FreeArgs (cont_rda);
  81.     }
  82.  
  83.     if (level)
  84.     {
  85.     fprintf (stderr, "Missing ELSE or ENDIF");
  86.     cond = 10;
  87.     }
  88.  
  89.     SelectInput (old_in);
  90.  
  91.     if (cont_rda)
  92.     FreeArgs (cont_rda);
  93.  
  94.     return (cond < 5) ? 0 : cond;
  95. } /* main */
  96.  
  97.